package com.mcxx.modules.extremepoverty.controller;

import com.mcxx.modules.extremepoverty.service.ExCustomFundPayService;
import com.mcxx.modules.miniSalvation.fundpay.dto.CustomFundPayDTO;
import com.mcxx.modules.miniSalvation.fundpay.dto.CustomFundPayMemberDTO;
import com.mcxx.modules.miniSalvation.fundpay.param.CustomFundPayListSearchParam;
import com.mcxx.modules.miniSalvation.fundpay.param.CustomFundPayMemberParam;
import com.mcxx.modules.miniSalvation.fundpay.service.read.CustomFundPayMemberReadService;
import com.mcxx.modules.miniSalvation.fundpay.service.read.CustomFundPayReadService;
import com.mcxx.modules.miniSalvation.fundpay.service.write.CustomFundPayMemberWriteService;
import com.mcxx.modules.miniSalvation.fundpay.service.write.CustomFundPayWriteService;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.Constant;
import com.mcxx.util.FileExportUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@Slf4j
@Validated
@Api("自定义补发名册")
@RestService({"extremePoverty/customFundpay/v2"})
public class ExCustomFundPayController extends DefaultRestService {
    @Autowired
    private ExCustomFundPayService customFundPayService;
    @Autowired
    private CustomFundPayMemberReadService customFundPayMemberReadService;
    @Autowired
    private CustomFundPayMemberWriteService customFundPayMemberWriteService;
    @Autowired
    private CustomFundPayReadService customFundPayReadService;
    @Autowired
    private CustomFundPayWriteService customFundPayWriteService;


    @PostMapping("getCustomFundPayList")
    @ApiOperation("自定义补发名册列表页查询")
    public void getCustomFundPayList(CustomFundPayListSearchParam customFundPayListSearchParam, PageParam pageParam){
        log.info("自定义补发名册列表页查询,参数：{}", customFundPayListSearchParam);
        Page page = customFundPayReadService.getCustomFundPayList(customFundPayListSearchParam, pageParam, getCurUserAccount());
        this.setPageBean(page);
        log.info("自定义补发名册列表页查询,结果：{}", page);
    }


    @PostMapping("addCustomFundPay")
    @ApiOperation("创建自定义补发名册")
    public void addCustomFundPay(@V CustomFundPayDTO customFundPayDTO){
        log.info("自定义补发名册列表页查询,参数：{}", customFundPayDTO);
        customFundPayDTO.setAreaCode(getCurUserAccount().getAreaCode());
        customFundPayDTO.setOrgId(getCurUserAccount().getOrgId());
        customFundPayService.addCustomFundPay(customFundPayDTO);
        setData(true);
        log.info("创建自定义补发名册,结果：{}", true);
    }


    @PostMapping("addCustomFundPayObjects")
    @ApiOperation("添加自定义补发名册对象")
    public void addCustomFundPayObjects(@V("notnull") String customFundPayId, @V("notnull") @RequestPart List<String> memberIdList){
        log.info("添加自定义补发名册对象,参数customFundPayId：{}, memberIdList:{}", customFundPayId, memberIdList);
        customFundPayService.addCustomFundPayObjects(customFundPayId, memberIdList, getCurUserAccount());
        setData(true);
        log.info("添加自定义补发名册对象结束");
    }


    @PostMapping("importCustomFundPayObjects")
    @ApiOperation("导入自定义补发名册对象")
    public void importCustomFundPayObjects(@RequestPart("file") MultipartFile file, @V("notnull") String customFundPayId){
        log.info("添加自定义补发名册对象,参数customFundPayId：{}, memberIdList:{}", customFundPayId);
        String rstMsg = customFundPayService.importCustomFundPayObjects(customFundPayId, file, getCurUserAccount());
        setData(rstMsg);
        log.info("添加自定义补发名册对象,结果：{}", rstMsg);
    }


    @PostMapping("delCustomFundPayObjects")
    @ApiOperation("删除自定义补发名册对象")
    public void delCustomFundPayObjects(@V("notnull") String customFundPayId, @V("notnull") @RequestPart List<String> memberIdList){
        log.info("删除自定义补发名册对象,参数customFundPayId：{}, memberIdList:{}", customFundPayId, memberIdList);
        int rst = customFundPayService.delCustomFundPayObjects(customFundPayId, memberIdList);
        setData(rst);
        log.info("删除自定义补发名册对象,结果：{}", rst);
    }


    @PostMapping("delCustomFundPay")
    @ApiOperation("删除自定义补发名册")
    public void delCustomFundPay(@V("notnull") @RequestPart List<String> customFundPayIdList){
        log.info("删除自定义补发名册,参数customFundPayIdList：{}", customFundPayIdList);

        Map<String, List<String>> rstMap = customFundPayService.delCustomFundPay(customFundPayIdList);

        setData(packageRstMsg(rstMap));
        log.info("删除自定义补发名册,结果：{}", rstMap);
    }


    @PostMapping("cancelCustomFundPay")
    @ApiOperation("撤销自定义补发名册")
    public void cancelCustomFundPay(@V("notnull") @RequestPart List<String> customFundPayIdList){
        log.info("撤销自定义补发名册,参数customFundPayIdList：{}", customFundPayIdList);

        Map<String, List<String>> rstMap = customFundPayService.cancelCustomFundPay(customFundPayIdList);

        setData(packageRstMsg(rstMap));
        log.info("撤销自定义补发名册,结果：{}", rstMap);
    }


    @PostMapping("getCustomFundPayDetail")
    @ApiOperation("查看自定义补发名册明细")
    public void getCustomFundPayDetail(CustomFundPayMemberParam customFundPayMemberParam, PageParam pageParam){
        log.info("查看自定义补发名册明细,参数customFundPayMemberParam：{}", customFundPayMemberParam);
        customFundPayMemberParam.setBusinessType(Constant.BusinessCode.TEKUN);
        Page page = customFundPayMemberReadService.getMemberByCustomFundPayId(customFundPayMemberParam, pageParam, getCurUserAccount());
        this.setPageBean(page);
        log.info("查看自定义补发名册明细,结果：{}", page);
    }


    @PostMapping("confirmCustomFundPay")
    @ApiOperation("确认自定义补发名册")
    public void confirmCustomFundPay(@V("notnull") @RequestPart List<String> customFundPayIdList){
        log.info("确认自定义补发名册,参数：{}");

        Map<String, List<String>> rstMap = customFundPayService.confirmCustomFundPay(customFundPayIdList, getCurUserAccount());

        setData(packageRstMsg(rstMap));
        log.info("确认自定义补发名册,结果：{}");
    }


    @PostMapping("updateMemberFundNumber")
    @ApiOperation("修改补发金额")
    public void updateMemberFundNumber(@V("notnull")CustomFundPayMemberDTO customFundPayMemberDTO){
        log.info("修改补发金额,参数customFundPayMemberDTO：{}", customFundPayMemberDTO);
        customFundPayService.updateMemberFundNumber(customFundPayMemberDTO);
        setData(true);
        log.info("修改补发金额,结果是：{}", true);
    }


    @PostMapping("updateMemberRemark")
    @ApiOperation("修改成员备注")
    public void updateMemberRemark(@V("notnull")CustomFundPayMemberDTO customFundPayMemberDTO){
        log.info("修改成员备注,参数customFundPayMemberDTO：{}", customFundPayMemberDTO);
        customFundPayMemberWriteService.updateById(customFundPayMemberDTO);
        setData(true);
        log.info("修改成员备注,结果是：{}", true);
    }


    @PostMapping({"downLoadExCustFundPayList"})
    public void downLoadExCustFundPayList(HttpServletResponse response, @RequestBody CustomFundPayListSearchParam param) throws IOException {
        param.setBusinessCode(Constant.BusinessCode.TEKUN);
        ExcelInfo excelInfo = customFundPayWriteService.exportExcelCustFundPay(param.getExportType(),param.getIdsList(),param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }


    private String packageRstMsg(Map<String, List<String>> rstMap){
        StringBuffer rstMsg = new StringBuffer();
        if(rstMap.containsKey("success")){
            rstMsg.append("成功【").append(rstMap.get("success").size()).append("】条\n");
        }
        if(rstMap.containsKey("fail")){
            rstMsg.append("失败【").append(rstMap.get("fail").size()).append("】条");
        }
        return rstMsg.toString();
    }



}
